package javapoker;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.util.InputMismatchException;



public class reka {
    private Karta[] karty;
    private Karta[] kartyBot1;
    private Karta[] kartyBot2;
    private Karta[] kartyBot3;
    private int[] value;

    reka(Talia d)
    {
        value = new int[6];
        karty = new Karta[10];
        kartyBot1 = new Karta[10];
        kartyBot2 = new Karta[20];
        kartyBot3 = new Karta[30];
        for (int x=0; x<5; x++)
        {
            karty[x] = d.drawFromDeck();
        }
        for (int x=0; x<5; x++)
        {
            kartyBot1[x] = d.drawFromDeck();
        }
        
        for (int x=0; x<5; x++)
        {
            kartyBot2[x] = d.drawFromDeck();
        }
        
        
        for (int x=0; x<5; x++)
        {
            kartyBot3[x] = d.drawFromDeck();
        }
        
        
        

        int[] ranks = new int[14];
        
        int[] orderedRanks = new int[5];
        boolean flush=true, straight=false;
        int takieSame=1,takieSame2=1;
        int largeGroupRank=0,smallGroupRank=0;
        int index=0;
        int topStraightValue=0;

        for (int x=0; x<=13; x++)
        {
            ranks[x]=0;
        }
        for (int x=0; x<=4; x++)
        {
            ranks[ karty[x].getRank() ]++;
        }
        for (int x=0; x<4; x++) {
            if ( karty[x].getSuit() != karty[x+1].getSuit() )
                flush=false;
        }

        for (int x=13; x>=1; x--)
        {
                 if (ranks[x] > takieSame)
                 {
                     if (takieSame != 1)
                     //jezeli takiesame nie byly domyslna wartoscia.
                     {
                    	 takieSame2 = takieSame;
                         smallGroupRank = largeGroupRank;
                     }

                     takieSame = ranks[x];
                     largeGroupRank = x;

                 } else if (ranks[x] > takieSame2)
                 {
                	 takieSame2 = ranks[x];
                     smallGroupRank = x;
                 }
        }

        if (ranks[1]==1) //jezeli wylosowal sie as zacznij od tego miejsca
        {
            orderedRanks[index]=14;
            index++;
        }

        for (int x=13; x>=2; x--)
        {
            if (ranks[x]==1)
            {
                orderedRanks[index]=x; //As
                index++;
            }
        }
        
        for (int x=1; x<=9; x++)
        //Strit moze zaczac sie od 10 w gore
        {
            if (ranks[x]==1 && ranks[x+1]==1 && ranks[x+2]==1 && 
                ranks[x+3]==1 && ranks[x+4]==1)
            {
                straight=true;
                topStraightValue=x+4; //4 karty za najnizsza
                break;
            }
        }

        if (ranks[10]==1 && ranks[11]==1 && ranks[12]==1 && 
            ranks[13]==1 && ranks[1]==1) //As najwyzszy
        {
            straight=true;
            topStraightValue=14; //Wyzszy niz krol
        }
        
        for (int x=0; x<=5; x++)
        {
            value[x]=0;
        }


        //start hand evaluation
        if ( takieSame==1 ) {
            value[0]=1;
            value[1]=orderedRanks[0];
            value[2]=orderedRanks[1];
            value[3]=orderedRanks[2];
            value[4]=orderedRanks[3];
            value[5]=orderedRanks[4];
        }

        if (takieSame==2 && takieSame2==1)
        {
            value[0]=2;
            value[1]=largeGroupRank; //para
            value[2]=orderedRanks[0];
            value[3]=orderedRanks[1];
            value[4]=orderedRanks[2];
        }

        if (takieSame==2 && takieSame2==2) //2 pary
        {
            value[0]=3;
            //najlepsza para
            value[1]= largeGroupRank>smallGroupRank ? largeGroupRank : smallGroupRank;
            value[2]= largeGroupRank<smallGroupRank ? largeGroupRank : smallGroupRank;
            value[3]=orderedRanks[0];  //extra card
        }

        if (takieSame==3 && takieSame2!=2)
        {
            value[0]=4;
            value[1]= largeGroupRank;
            value[2]=orderedRanks[0];
            value[3]=orderedRanks[1];
        }

        if (straight && !flush)
        {
            value[0]=5;
            value[1]=0;
        }

        if (flush && !straight)
        {
            value[0]=6;
            value[1]=orderedRanks[0]; //tie determined by ranks of karty
            value[2]=orderedRanks[1];
            value[3]=orderedRanks[2];
            value[4]=orderedRanks[3];
            value[5]=orderedRanks[4];
        }

        if (takieSame==3 && takieSame2==2)
        {
            value[0]=7;
            value[1]=largeGroupRank;
            value[2]=smallGroupRank;
        }

        if (takieSame==4)
        {
            value[0]=8;
            value[1]=largeGroupRank;
            value[2]=orderedRanks[0];
        }

        if (straight && flush)
        {
            value[0]=9;
            value[1]=0;
        }

    }

    void display()
    {
        String s;
        switch( value[0] )
        {

            case 1:
                s="Najwyzsza karta";
                break;
            case 2:
                s="Para:  " + Karta.rankAsString(value[1]) + "\'s";
                break;
            case 3:
                s="dwie pary:  " + Karta.rankAsString(value[1]) + " " + 
                		Karta.rankAsString(value[2]);
                break;
            case 4:
                s="Trojka " + Karta.rankAsString(value[1]) + "\'s";
                break;
            case 5:
                s=Karta.rankAsString(value[1]) + " Duzy Strit";
                break;
            case 6:
                s="Kolor";
                break;
            case 7:
                s="Full " + Karta.rankAsString(value[1]) + " Na " + 
                		Karta.rankAsString(value[2]);
                break;
            case 8:
                s="Kareta: " + Karta.rankAsString(value[1]);
                break;
            case 9:
                s="Poker! " + Karta.rankAsString(value[1]) + " Najwyzsza karta: ";
                //p="Poker! " + Karta.rankAsString(value[1]) + " Najwyzsza karta: ";
                break;
            default:
                s="Blad wyswietlania: value[0] zawiera nieprawidlowa wartosc";
        }
        s = "                " + s;
        System.out.println(s);
    }

    void displayAll()
    {
        int x=0;
        int q=0;
        
	
    	    		
    //	}
        System.out.println("moje karty\n");
    	for (x=0; x<5; x++){
    		
    		
            System.out.println(karty[x]);
            
           
    	}
    	
    	 System.out.println( );
    	 System.out.println("karty bota nr 1: \n" );
    	for (x=0; x<5; x++){
    	
    	System.out.println(kartyBot1[x]);
    	}
    	
    	System.out.println( );
    	
    	System.out.println("karty bota nr 2: \n" );
    	for (x=0; x<5; x++){
        	System.out.println(kartyBot2[x]);
        	}
    	
    	System.out.println( );
 	System.out.println("karty bota nr 3: \n" );
    	for (x=0; x<5; x++){
        	System.out.println(kartyBot3[x]);
        	}
    	
    	    
       
        
        
        
        //////////////////////////// drugie rozdanie, po wymianie ///////////////////////////////
        
    	
  	  System.out.println("czy chcesz wymienic karty? 0/1   0 - nie, 1 - tak" );
      
      Scanner decyzja = new Scanner(System.in);
		int m = decyzja.nextInt();
      if(m==1)
      {
      	System.out.println("czy chcesz wymienic pierwsza karte?");
      	Scanner k1 = new Scanner(System.in);
      	int k11 = k1.nextInt();
      	if(k11==1)
      	{
      		karty[0] = Talia.karty.get(6);
      	}
      	else
      	{
      		try
      		{
      			karty[0]=karty[0];
      		}
      		catch(InputMismatchException ex){}
      	}
      	
      	System.out.println("czy chcesz wymienic druga karte?");
      	Scanner k2 = new Scanner(System.in);
      	int k12 = k2.nextInt();
      	if(k11==1)
      	{
      		karty[1] = Talia.karty.get(7);
      	}
      	else
      	{
      		try
      		{
      			karty[1]=karty[1];
      		}
      		catch(InputMismatchException ex){}
      	}
      	
      	System.out.println("czy chcesz wymienic trzecia karte?");
      	Scanner k3 = new Scanner(System.in);
      	int k13 = k3.nextInt();
      	if(k13==1)
      	{
      		karty[2] = Talia.karty.get(8);
      	}
      	else
      	{
      		try
      		{
      			karty[2]=karty[2];
      		}
      		catch(InputMismatchException ex){}
      	}
      	
      	System.out.println("czy chcesz wymienic czwarta karte?");
      	Scanner k4 = new Scanner(System.in);
      	int k14 = k4.nextInt();
      	if(k14==1)
      	{
      		karty[3] = Talia.karty.get(9);
      	}
      	else
      	{
      		try
      		{
      			karty[3]=karty[3];
      		}
      		catch(InputMismatchException ex){}
      	}
      	
      	System.out.println("czy chcesz wymienic piata karte?");
      	Scanner k5 = new Scanner(System.in);
      	int k15 = k5.nextInt();

      	if(k15==1 && k11==1 && k12==1 && k13==1 && k14==1)
      	{
      		System.out.println("Juz wymieniles 4 karty");
      		karty[4]=karty[4];
      		System.out.println();
      	}
      	else if(k15==0)
      	{
      		try
      		{
      			karty[4]=karty[4];
      		}
      		catch(InputMismatchException ex){}
      	}
      	else
      	{
      		karty[4] = Talia.karty.get(10);
      	}
      	
      	
      	 System.out.println("\n ---- WYMIANA ---- \n");
      	System.out.println();
      	System.out.println("Twoje koncowe karty:");
      	System.out.println(karty[0]);
          System.out.println(karty[1]);    /// wsywietlanie reki po wymianie
          System.out.println(karty[2]);
          System.out.println(karty[3]);
          System.out.println(karty[4]);
          System.out.println( );
      	
      }
      else
      {
      	System.out.println();
      	System.out.println("Twoje koncowe karty:");
      	System.out.println(karty[0]);
          System.out.println(karty[1]);    /// wsywietlanie reki po wymianie
          System.out.println(karty[2]);
          System.out.println(karty[3]);
          System.out.println(karty[4]);
          System.out.println( );
          
      }
		
  



    	
    	
    	
    	
       
        
       
       
        
        
        
        Random generator1 = new Random();
        
        
        int y = generator1.nextInt(5);         ///// losujemy randomowo liczbe ile kart bot chce wymienic (jezeli wylosuje sie 0 to petla w ogole sie nie obroci)
        
        
        for(int z=0; z<y; z++){
        	
        int t = generator1.nextInt(5);   // losujemy randomowo pozycje karty którą bot chce wymienic 
        kartyBot1[t] = Talia.karty.get(t+1);
        
        
        }
        System.out.println(" bot1 po wymianie kart: \n" );
        for (x=0; x<5; x++){
        	
        	System.out.println(kartyBot1[x]);
        	}
        	
        	
        	
        	
        
        Random generator2 = new Random();
        	int l = 5;
        	int w = generator2.nextInt(5);         ///// losujemy randomowo liczbe ile kart bot chce wymienic (jezeli wylosuje sie 0 to petla w ogole sie nie obroci)
            
        	//System.out.println(w);
            for(int e=0; e<w; e++){
            	
            int r = generator2.nextInt(5);
            
            kartyBot2[r] = Talia.karty.get(r+7);
//            l=l+1;
            
            
            }
            System.out.println( );
        	 System.out.println(" bot2 po wymianie kart: \n" );
        	 
        	for (x=0; x<5; x++){
            	System.out.println(kartyBot2[x]);
            	}
        	
        	System.out.println( );
        	
        	
        	int d = generator2.nextInt(5);
       // 	System.out.println(d);
        	for(int s=0; s<d; s++){
            	
                int f = generator2.nextInt(5);   // losujemy randomowo pozycje karty którą bot chce wymienic 
                kartyBot3[f] = Talia.karty.get(f+1);
                
                
                }
                System.out.println( );
            	 System.out.println(" bot3 po wymianie kart: \n" );
            	 
            	for (x=0; x<5; x++){
                	System.out.println(kartyBot3[x]);
                	}
            	
            	System.out.println( );

        	
    }
        	

        
        
        
        
        
 
    

    int compareTo(reka that)
    {
        for (int x=0; x<6; x++)
        {
            if (this.value[x]>that.value[x])
                return 1;
            else if (this.value[x]<that.value[x])
                return -1;
        }
        return 0; //jezeli karty na rece sa takie same
    }
}